Skip to content

Commit eebe00b

Browse files
committed
Implement PWMOUT
1 parent 2a06ae2 commit eebe00b

File tree

13 files changed

+3333
-126
lines changed

13 files changed

+3333
-126
lines changed

targets/TARGET_TI/TARGET_CC32XX/TARGET_CC3220SF/TARGET_CC3220SF_LAUNCHXL/CC3220SF_LAUNCHXL.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,70 @@ const SPI_Config SPI_config[CC3220SF_LAUNCHXL_SPICOUNT] = {
215215

216216
const uint_least8_t SPI_count = CC3220SF_LAUNCHXL_SPICOUNT;
217217

218+
/*
219+
* =============================== PWM ===============================
220+
*/
221+
#include <ti/drivers/PWM.h>
222+
#include <ti/drivers/pwm/PWMTimerCC32XX.h>
223+
224+
PWMTimerCC32XX_Object pwmTimerCC3220SObjects[CC3220SF_LAUNCHXL_PWMCOUNT];
225+
226+
const PWMTimerCC32XX_HWAttrsV2 pwmTimerCC3220SHWAttrs[CC3220SF_LAUNCHXL_PWMCOUNT] = {
227+
{ /* CC3220SF_LAUNCHXL_PWM6 */
228+
.pwmPin = PWMTimerCC32XX_PIN_01
229+
},
230+
{ /* CC3220SF_LAUNCHXL_PWM7 */
231+
.pwmPin = PWMTimerCC32XX_PIN_02
232+
},
233+
{ /* CC3220SF_LAUNCHXL_PWM0 */
234+
.pwmPin = PWMTimerCC32XX_PIN_17
235+
},
236+
{ /* CC3220SF_LAUNCHXL_PWM3 */
237+
.pwmPin = PWMTimerCC32XX_PIN_19
238+
},
239+
{ /* CC3220SF_LAUNCHXL_PWM2 */
240+
.pwmPin = PWMTimerCC32XX_PIN_21
241+
},
242+
{ /* CC3220SF_LAUNCHXL_PWM5 */
243+
.pwmPin = PWMTimerCC32XX_PIN_64
244+
},
245+
};
246+
247+
const PWM_Config PWM_config[CC3220SF_LAUNCHXL_PWMCOUNT] = {
248+
{
249+
.fxnTablePtr = &PWMTimerCC32XX_fxnTable,
250+
.object = &pwmTimerCC3220SObjects[CC3220SF_LAUNCHXL_PWM6],
251+
.hwAttrs = &pwmTimerCC3220SHWAttrs[CC3220SF_LAUNCHXL_PWM6]
252+
},
253+
{
254+
.fxnTablePtr = &PWMTimerCC32XX_fxnTable,
255+
.object = &pwmTimerCC3220SObjects[CC3220SF_LAUNCHXL_PWM7],
256+
.hwAttrs = &pwmTimerCC3220SHWAttrs[CC3220SF_LAUNCHXL_PWM7]
257+
},
258+
{
259+
.fxnTablePtr = &PWMTimerCC32XX_fxnTable,
260+
.object = &pwmTimerCC3220SObjects[CC3220SF_LAUNCHXL_PWM0],
261+
.hwAttrs = &pwmTimerCC3220SHWAttrs[CC3220SF_LAUNCHXL_PWM0]
262+
},
263+
{
264+
.fxnTablePtr = &PWMTimerCC32XX_fxnTable,
265+
.object = &pwmTimerCC3220SObjects[CC3220SF_LAUNCHXL_PWM3],
266+
.hwAttrs = &pwmTimerCC3220SHWAttrs[CC3220SF_LAUNCHXL_PWM3]
267+
},
268+
{
269+
.fxnTablePtr = &PWMTimerCC32XX_fxnTable,
270+
.object = &pwmTimerCC3220SObjects[CC3220SF_LAUNCHXL_PWM2],
271+
.hwAttrs = &pwmTimerCC3220SHWAttrs[CC3220SF_LAUNCHXL_PWM2]
272+
},
273+
{
274+
.fxnTablePtr = &PWMTimerCC32XX_fxnTable,
275+
.object = &pwmTimerCC3220SObjects[CC3220SF_LAUNCHXL_PWM5],
276+
.hwAttrs = &pwmTimerCC3220SHWAttrs[CC3220SF_LAUNCHXL_PWM5]
277+
}
278+
};
279+
280+
const uint_least8_t PWM_count = CC3220SF_LAUNCHXL_PWMCOUNT;
281+
218282
/*
219283
* =============================== DMA ===============================
220284
*/

targets/TARGET_TI/TARGET_CC32XX/TARGET_CC3220SF/TARGET_CC3220SF_LAUNCHXL/CC3220SF_LAUNCHXL.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ typedef enum CC3220SF_LAUNCHXL_I2SName {
138138
typedef enum CC3220SF_LAUNCHXL_PWMName {
139139
CC3220SF_LAUNCHXL_PWM6 = 0,
140140
CC3220SF_LAUNCHXL_PWM7,
141-
141+
CC3220SF_LAUNCHXL_PWM0,
142+
CC3220SF_LAUNCHXL_PWM3,
143+
CC3220SF_LAUNCHXL_PWM2,
144+
CC3220SF_LAUNCHXL_PWM5,
142145
CC3220SF_LAUNCHXL_PWMCOUNT
143146
} CC3220SF_LAUNCHXL_PWMName;
144147

targets/TARGET_TI/TARGET_CC32XX/TARGET_CC3220SF/device/CC3220SF_WiFiInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ void CC3220SFInterface::_socket_background_thread()
588588
}
589589
_mutex.unlock();
590590
}
591-
wait_ms(READ_THREAD_SLEEP_MS);
591+
ThisThread::sleep_for(READ_THREAD_SLEEP_MS);
592592
}
593593
}
594594

targets/TARGET_TI/TARGET_CC32XX/TARGET_CC3220SF/device/cc3200_simplelink.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ int CC3200_SIMPLELINK::scan(WiFiAccessPoint *res, unsigned count)
378378
while(triggeredScanTrials < MAX_SCAN_ATTEMPTS)
379379
{
380380
/* We wait for one second for the NWP to complete the initiated scan and collect results */
381-
wait_ms(1000);
381+
ThisThread::sleep_for(1000);
382382

383383
/* Collect results form one-shot scans.*/
384384
ret = sl_WlanGetNetworkList(0, entries_count, netEntries);
@@ -663,7 +663,7 @@ nsapi_error_t CC3200_SIMPLELINK::connect_socket(uint32_t sd, const SocketAddress
663663
{
664664
if (status == SL_ERROR_BSD_EALREADY && 1 == nonBlocking)
665665
{
666-
wait_ms(1);
666+
ThisThread::sleep_for(1);
667667
continue;
668668
}
669669
else if (status < 0)
@@ -711,7 +711,7 @@ int CC3200_SIMPLELINK::sendto_socket(uint32_t sd, const void * buf, uint32_t buf
711711
status = sl_SendTo(sd, buf, bufLen, 0, sa, addrSize);
712712
if (status == SL_ERROR_BSD_EAGAIN && 1 == SOCKET_IS_NON_BLOCKING)
713713
{
714-
wait_ms(1);
714+
ThisThread::sleep_for(1);
715715
continue;
716716
}
717717
else if (status < 0)
@@ -733,7 +733,7 @@ int32_t CC3200_SIMPLELINK::send(int sd, const void *data, uint32_t size)
733733
status = sl_Send(sd, data, size, 0);
734734
if (status == SL_ERROR_BSD_EAGAIN && 1 == SOCKET_IS_NON_BLOCKING)
735735
{
736-
wait_ms(1);
736+
ThisThread::sleep_for(1);
737737
continue;
738738
}
739739
else if (status < 0)

targets/TARGET_TI/TARGET_CC32XX/TARGET_CC3220SF/objects.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ struct gpio_irq_s {
4040
uint32_t port;
4141
PinName pin;
4242
uint32_t ch;
43-
unsigned long pin_mask;
44-
unsigned long irq_offset;
43+
unsigned long pin_mask;
44+
unsigned long irq_offset;
4545
};
4646

4747
struct port_s {
@@ -52,7 +52,9 @@ struct port_s {
5252
};
5353

5454
struct pwmout_s {
55-
uint32_t pwmPin;
55+
uint32_t period_us;
56+
float duty_percent;
57+
void * handle;
5658
PWMName pwm;
5759
};
5860

targets/TARGET_TI/TARGET_CC32XX/TARGET_CC3220SF/pwmout_api.c

Lines changed: 63 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -15,148 +15,89 @@
1515
*/
1616
#include "mbed_assert.h"
1717
#include "pwmout_api.h"
18-
#include "cmsis.h"
1918
#include "pinmap.h"
2019
#include "PeripheralPins.h"
2120

22-
#include <ti/devices/cc32xx/inc/hw_apps_config.h>
23-
#include <ti/devices/cc32xx/inc/hw_ocp_shared.h>
24-
#include <ti/devices/cc32xx/inc/hw_types.h>
25-
#include <ti/devices/cc32xx/inc/hw_timer.h>
26-
#include <ti/devices/cc32xx/inc/hw_memmap.h>
27-
#include <ti/devices/cc32xx/driverlib/rom.h>
28-
#include <ti/devices/cc32xx/driverlib/rom_map.h>
29-
#include <ti/devices/cc32xx/driverlib/gpio.h>
30-
#include <ti/devices/cc32xx/driverlib/pin.h>
31-
#include <ti/devices/cc32xx/driverlib/ti_timer.h>
32-
33-
static const uint32_t timerBaseAddresses[4] = {
34-
CC3220SF_TIMERA0_BASE,
35-
CC3220SF_TIMERA1_BASE,
36-
CC3220SF_TIMERA2_BASE,
37-
CC3220SF_TIMERA3_BASE,
38-
};
39-
40-
static const uint32_t timerHalves[2] = {
41-
TIMER_A,
42-
TIMER_B,
43-
};
44-
45-
/*static const uint32_t gpioBaseAddresses[4] = {
46-
CC3220SF_GPIOA0_BASE,
47-
CC3220SF_GPIOA1_BASE,
48-
CC3220SF_GPIOA2_BASE,
49-
CC3220SF_GPIOA3_BASE,
50-
};*/
51-
52-
/*static const uint32_t gpioPinIndexes[8] = {
53-
GPIO_PIN_0,
54-
GPIO_PIN_1,
55-
GPIO_PIN_2,
56-
GPIO_PIN_3,
57-
GPIO_PIN_4,
58-
GPIO_PIN_5,
59-
GPIO_PIN_6,
60-
GPIO_PIN_7,
61-
};*/
62-
63-
#define PinConfigTimerPort(config) (((config) >> 28) & 0xF)
64-
#define PinConfigTimerHalf(config) (((config) >> 24) & 0xF)
65-
#define PinConfigGPIOPort(config) (((config) >> 20) & 0xF)
66-
#define PinConfigGPIOPinIndex(config) (((config) >> 16) & 0xF)
67-
#define PinConfigPinMode(config) (((config) >> 8) & 0xF)
68-
#define PinConfigPin(config) (((config) >> 0) & 0x3F)
69-
70-
#define PWMTimerCC32XX_T0A (0x00 << 24)
71-
#define PWMTimerCC32XX_T0B (0x01 << 24)
72-
#define PWMTimerCC32XX_T1A (0x10 << 24)
73-
#define PWMTimerCC32XX_T1B (0x11 << 24)
74-
#define PWMTimerCC32XX_T2A (0x20 << 24)
75-
#define PWMTimerCC32XX_T2B (0x21 << 24)
76-
#define PWMTimerCC32XX_T3A (0x30 << 24)
77-
#define PWMTimerCC32XX_T3B (0x31 << 24)
78-
79-
#define PWMTimerCC32XX_GPIO9 (0x11 << 16)
80-
#define PWMTimerCC32XX_GPIO10 (0x12 << 16)
81-
#define PWMTimerCC32XX_GPIO11 (0x13 << 16)
82-
#define PWMTimerCC32XX_GPIO24 (0x30 << 16)
83-
#define PWMTimerCC32XX_GPIO25 (0x31 << 16)
84-
85-
#define PWMTimerCC32XX_GPIONONE (0xFF << 16)
86-
87-
#define PWMTimerCC32XX_PIN_01 (PWMTimerCC32XX_T3A | PWMTimerCC32XX_GPIO10 | 0x0300)
88-
#define PWMTimerCC32XX_PIN_02 (PWMTimerCC32XX_T3B | PWMTimerCC32XX_GPIO11 | 0x0301)
89-
#define PWMTimerCC32XX_PIN_17 (PWMTimerCC32XX_T0A | PWMTimerCC32XX_GPIO24 | 0x0510)
90-
#define PWMTimerCC32XX_PIN_19 (PWMTimerCC32XX_T1B | PWMTimerCC32XX_GPIONONE | 0x0812)
91-
#define PWMTimerCC32XX_PIN_21 (PWMTimerCC32XX_T1A | PWMTimerCC32XX_GPIO25 | 0x0914)
92-
#define PWMTimerCC32XX_PIN_64 (PWMTimerCC32XX_T2B | PWMTimerCC32XX_GPIO9 | 0x033F)
93-
94-
//static unsigned int pwm_clock_mhz;
21+
#include <ti/drivers/pwm/PWMTimerCC32XX.h>
22+
#include <ti/drivers/PWM.h>
23+
#include <CC3220SF_LAUNCHXL.h>
24+
25+
extern const PWM_Config PWM_config[];
9526

9627
void pwmout_init(pwmout_t* obj, PinName pin) {
28+
PWM_Params pwmParams;
29+
int pwmIndex = CC3220SF_LAUNCHXL_PWMCOUNT;
30+
31+
PWM_init();
32+
9733
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
9834
MBED_ASSERT(pwm != (PWMName)NC);
35+
9936
obj->pwm = pwm;
10037

10138
switch(pin) {
102-
case PIN_01: obj->pwmPin = PWMTimerCC32XX_PIN_01; break;
103-
case PIN_02: obj->pwmPin = PWMTimerCC32XX_PIN_02; break;
104-
case PIN_17: obj->pwmPin = PWMTimerCC32XX_PIN_17; break;
105-
case PIN_19: obj->pwmPin = PWMTimerCC32XX_PIN_19; break;
106-
case PIN_21: obj->pwmPin = PWMTimerCC32XX_PIN_21; break;
107-
case PIN_64: obj->pwmPin = PWMTimerCC32XX_PIN_64; break;
108-
default: break;
109-
}
39+
case PIN_01:
40+
pwmIndex = CC3220SF_LAUNCHXL_PWM6;
41+
break;
11042

111-
uint32_t timerBaseAddr = timerBaseAddresses[PinConfigTimerPort(obj->pwmPin)];
112-
uint16_t halfTimer = timerHalves[PinConfigTimerHalf(obj->pwmPin)];
113-
114-
MAP_TimerDisable(timerBaseAddr, halfTimer);
115-
116-
/*
117-
* The CC32XX SDK TimerConfigure API halts both timers when it is
118-
* used to configure a single half timer. The code below performs
119-
* the register operations necessary to configure each half timer
120-
* individually.
121-
*/
122-
/* Enable CCP to IO path */
123-
HWREG(APPS_CONFIG_BASE + APPS_CONFIG_O_GPT_TRIG_SEL) = 0xFF;
124-
125-
/* Split the timer and configure it as a PWM */
126-
uint32_t timerConfigVal = ((halfTimer & (TIMER_CFG_A_PWM | TIMER_CFG_B_PWM)) |
127-
TIMER_CFG_SPLIT_PAIR);
128-
HWREG(timerBaseAddr + TIMER_O_CFG) |= (timerConfigVal >> 24);
129-
if (halfTimer & TIMER_A) {
130-
HWREG(timerBaseAddr + TIMER_O_TAMR) = timerConfigVal & 255;
131-
}
132-
else {
133-
HWREG(timerBaseAddr + TIMER_O_TBMR) = (timerConfigVal >> 8) & 255;
43+
case PIN_02:
44+
pwmIndex = CC3220SF_LAUNCHXL_PWM7;
45+
break;
46+
47+
case PIN_17:
48+
pwmIndex = CC3220SF_LAUNCHXL_PWM0;
49+
break;
50+
51+
case PIN_19:
52+
pwmIndex = CC3220SF_LAUNCHXL_PWM3;
53+
break;
54+
55+
case PIN_21:
56+
pwmIndex = CC3220SF_LAUNCHXL_PWM2;
57+
break;
58+
59+
case PIN_64:
60+
pwmIndex = CC3220SF_LAUNCHXL_PWM5;
61+
break;
62+
63+
default:
64+
while(1);
13465
}
13566

136-
/* Set the peripheral output to active-high */
137-
MAP_TimerControlLevel(timerBaseAddr, halfTimer, true);
67+
obj->handle = (void *)&PWM_config[pwmIndex];
68+
69+
// Initialize the PWM parameters
70+
PWM_Params_init(&pwmParams);
13871

139-
uint16_t mode = PinConfigPinMode(obj->pwmPin);
72+
obj->duty_percent = PWM_DEFAULT_DUTY_PERCENT;
73+
obj->period_us = PWM_DEFAULT_PERIOD_US;
14074

141-
/* Start the timer & set pinmux to PWM mode */
142-
MAP_TimerEnable(timerBaseAddr, halfTimer);
143-
MAP_PinTypeTimer((unsigned long)pin, (unsigned long)mode);
75+
if (PWM_open(pwmIndex, &pwmParams))
76+
{
77+
PWM_start((PWM_Handle)obj->handle);
78+
}
79+
else
80+
{
81+
while(1);
82+
}
14483
}
14584

14685
void pwmout_free(pwmout_t* obj) {
147-
// [TODO]
86+
PWM_stop((PWM_Handle)obj->handle);
87+
PWM_close((PWM_Handle)obj->handle);
14888
}
14989

15090
void pwmout_write(pwmout_t* obj, float value) {
151-
91+
PWM_setDuty((PWM_Handle)obj->handle, value*100);
92+
obj->duty_percent = value;
15293
}
15394

15495
float pwmout_read(pwmout_t* obj) {
155-
return 0;
96+
return (obj->duty_percent);
15697
}
15798

15899
void pwmout_period(pwmout_t* obj, float seconds) {
159-
pwmout_period_us(obj, seconds * 1000000.0f);
100+
pwmout_period_us(obj, seconds * 1000 * 1000);
160101
}
161102

162103
void pwmout_period_ms(pwmout_t* obj, int ms) {
@@ -165,7 +106,8 @@ void pwmout_period_ms(pwmout_t* obj, int ms) {
165106

166107
// Set the PWM period, keeping the duty cycle the same.
167108
void pwmout_period_us(pwmout_t* obj, int us) {
168-
109+
PWM_setPeriod((PWM_Handle)obj->handle, us);
110+
obj->period_us = us;
169111
}
170112

171113
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
@@ -177,7 +119,11 @@ void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
177119
}
178120

179121
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
180-
122+
if (obj->period_us)
123+
{
124+
float value = (float)us / (float)obj->period_us;
125+
pwmout_write(obj, value);
126+
}
181127
}
182128

183129
const PinMap *pwmout_pinmap()

0 commit comments

Comments
 (0)